//
//  GameOverLayer.m
//  GameProject
//
//  Created by Blair Trusler on 18/01/2012.
//  Copyright (c) 2012 Blair Trusler. All rights reserved.
//

#import "GameHeader.h"

@implementation GameOverLayer


-(id)init{
	self = [super init];
	if (self != nil) {
        CCLOG(@"crash");
		// Accept touch input
		self.isTouchEnabled = YES;

        [AudioManager gameOverMusic];
        CCLOG(@"crash1");

		CGSize screenSize = [CCDirector sharedDirector].winSize;
                
        CCSprite *titleText = [CCSprite spriteWithFile:@"LevelFailed.png"];
        CCSprite *textBody = [CCSprite spriteWithFile:@"LevelFailedFull.png"];
        
        CCLOG(@"crash2");

        CCMenuItemImage *returnToMenuButton = [CCMenuItemImage itemFromNormalImage:@"ReturnToMenu.png" selectedImage:@"ReturnToMenu1.png" target:self selector:@selector(returnMenu:)];
        CCMenuItemImage *retryLevelButton = [CCMenuItemImage itemFromNormalImage:@"RetryLevel.png" selectedImage:@"RetryLevel1.png" target:self selector:@selector(retryLevel:)];
        
        CCMenu *returnMenu = [CCMenu menuWithItems:returnToMenuButton, nil];
        CCMenu *retryMenu =[CCMenu menuWithItems:retryLevelButton, nil];
        
        [self rewardAchievements];

        CCLOG(@"crash3");

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
            CCLOG(@"crash4");

            titleText.position = ccp(screenSize.width/2,700);
            textBody.position = ccp (500,500);
            returnMenu.position = ccp(500,300);
            retryMenu.position = ccp(500,150);
            
        }
        else{
            CCLOG(@"crash SHOULDNT");

            titleText.scale = 0.5f;
            titleText.position = ccp(screenSize.width/2,280);
            textBody.scale = 0.5f;
            textBody.position = ccp (screenSize.width/2,190);
            returnMenu.scale = 0.5f;
            retryMenu.scale = 0.5f;
            returnMenu.position = ccp(130,15);
            retryMenu.position = ccp(130,-35);
        }
        [self addChild:titleText];
        [self addChild:textBody];
        [self addChild:returnMenu];
        [self addChild:retryMenu];

	}
	return self;
}

-(void)rewardAchievements{
    //Award achievement 
    if (![GameState sharedInstance].achievement7){
        [GameState sharedInstance].achievement7 = TRUE;
        [[GameState sharedInstance]save];
        [[GameCenterLogin sharedInstance] reportAchievement:kAchievement7 percentComplete:100.0];
//        NSString *achievementName = [NSString stringWithFormat:kAchievement7];
//        CCLOG(@"Awarded Achievement - %@",achievementName);
    }
}


-(void)returnMenu:(id) sender{
    [[SceneManager sharedSceneManager] runSceneWithID:kMainMenuScene];    
}


-(void)retryLevel:(id) sender{
    CCLOG(@"retry level!");
    if (levelPlayed == 1){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel1];            
    }
    if (levelPlayed == 2){
        [[CCDirector sharedDirector] popScene];
        
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel2];            
    }
    if (levelPlayed == 3){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel3];            
    }
    
    if (levelPlayed == 4){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel4];            
    }
    if (levelPlayed == 5){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel5];            
    }

}

-(void) dealloc{
    [super dealloc];
}

@end
